﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Web.UI.Controls;
using DP.Common;

namespace CRM.Web.FaqManage
{
    public partial class FaqDirectManageRightContent : Common.PageBase
    {
        T_FAQ_TREE_BLL tBll = new T_FAQ_TREE_BLL();
        T_FAQ_SRC_BLL sBll = new T_FAQ_SRC_BLL();

        private UserLoginInfo _loginInfo;
        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            UserLoginInfo.CheckLoginInfo();
            _loginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageDataInit();
            }
        }

        private void pageDataInit()
        {
            T_FAQ_TREE_Entity entity = new T_FAQ_TREE_Entity();
            string catid = PageHelper.GetQueryString(this.Page, "CATID");
            entity = tBll.GetEntity("CATID", catid);
            if (entity == null)
            {
                this.txtCATID.Text = catid;
                this.txtCATNAME.Text = PageHelper.GetQueryString(this.Page, "CATNAME");
                this.txtBUSINESSDIVISIONCODE.Text = _loginInfo.UserEntity.BUSINESSDIVISIONCODE;
                this.txtDEPARTMENTCODE.Text = _loginInfo.UserEntity.DEPARTMENTCODE;

                btnAdd.Visible = false;
                btnOK.Visible = false;
                btnDelete.Visible = false;
                if (catid == "0")
                {
                    btnOK.Visible = false;
                    btnAdd.Visible = true;
                }
            }
            else
            {
                PageHelper.SetControlValue(this.Page, entity);
                this.lblTitle.Text = "FAQ节点编辑 >> " + PageHelper.GetQueryString(this.Page, "CATNAME");
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            btnAdd.Visible = false;
            btnOK.Visible = true;
            string catid = PageHelper.GetQueryString(this.Page, "CATID");
            if(!string.IsNullOrEmpty(catid))
            {
                PageHelper.ClearControlValue(this.Page);
                txtFID.Text = catid;
                rbISACTIVE1.Checked = true;
                //生成菜单节点
                T_FAQ_TREE_Entity entity = new T_FAQ_TREE_Entity();
                string strMaxCatid = tBll.GetMaxValue("CATID", string.Format(" AND FID='{0}'", catid));
                if (strMaxCatid == "")
                    strMaxCatid = catid + string.Format("{0:D2}", 0);
                if (StringHelper.ToInt(catid) == 0)
                {
                    txtCATID.Text = (StringHelper.ToInt(strMaxCatid) + 1).ToString();
                    txtORDERNUM.Text = (StringHelper.ToInt(strMaxCatid) + 1).ToString();
                }
                else
                {
                    txtCATID.Text = catid + string.Format("{0:D2}", long.Parse(strMaxCatid.Substring(catid.Length)) + 1);
                    txtORDERNUM.Text = catid + string.Format("{0:D2}", long.Parse(strMaxCatid.Substring(catid.Length)) + 1);
                }

                this.txtBUSINESSDIVISIONCODE.Text = _loginInfo.UserEntity.BUSINESSDIVISIONCODE;
                this.txtDEPARTMENTCODE.Text = _loginInfo.UserEntity.DEPARTMENTCODE;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            btnAdd.Visible = true;
            if (string.IsNullOrEmpty(txtCATNAME.Text))
            {
                PageHelper.ShowMessage("菜单名称不能为空！");
                return;
            }
            T_FAQ_TREE_Entity entity = new T_FAQ_TREE_Entity();
            entity = tBll.GetEntity("CATID", txtCATID.Text);
            try
            {
                if (entity == null)
                {
                    entity = new T_FAQ_TREE_Entity();
                    PageHelper.GetControlValue(this.Page, ref entity);
                    entity.BUSINESSDIVISIONCODE = _loginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    entity.DEPARTMENTCODE = _loginInfo.UserEntity.DEPARTMENTCODE;
                    tBll.Insert(entity);
                }
                else
                {
                    PageHelper.GetControlValue(this.Page, ref entity);
                    tBll.Update(entity);
                }
                PageHelper.RefreshParent("alert('保存成功！');");
                //PageHelper.ShowMessage(this.Page, "保存成功！");
            }
            catch (Exception ex)
            {
                PageHelper.ShowMessage(this.Page, ex.Message);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            T_FAQ_TREE_Entity entity = new T_FAQ_TREE_Entity();
            entity.CATID = StringHelper.ToInt(txtCATID.Text);
            try
            {
                long rel = tBll.Delete(entity);
                if (rel > 0)
                {
                    sBll.Delete("CATID", entity.CATID.ToString());
                    PageHelper.RefreshParent("alert('删除成功！');");
                    //PageHelper.ShowMessage(this.Page, "删除成功！");
                    //Response.Redirect(HttpContext.Current.Request.RawUrl);
                    //PageHelper.ShowMessage("删除成功！");
                }
            }
            catch (Exception ex)
            {
                PageHelper.ShowMessage(this.Page, ex.Message);
            }

        }
    }
}
